<?php

// 阿里云账户分组
class GroupsController extends AdminController
{
    // 列表
    public function listAction()
    {
        $do = $this->get('do');
        switch ($do) {
            case 'list':
                $parameters['conditions'] = array();
                $data = $this->_list(DunGroupsModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'title' => $item->title,
                        'account' => $item->account,
                        'accessKeyId' => $item->accessKeyId,
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
    }

    // 获取余额
    function walletAction()
    {

//                    $alibaba = new Aliyun($regionid, $item->accessKeyId, $item->accessSecret);
//                    $alibaba->setHost('BssOpenApi');
//                    $alibaba_result = $alibaba->QueryAccountBalance();
//                    if ($alibaba_result && $alibaba_result['Success'] == true) {
//                        $AvailableAmount = $alibaba_result['Data']['AvailableAmount'] .' '. $alibaba_result['Data']['Currency'];
//                        $AvailableCashAmount = $alibaba_result['Data']['AvailableCashAmount'] .' '. $alibaba_result['Data']['Currency'];
//                    }
//                    $alibaba = null;

    }

    // 新增编辑
    function saveAction()
    {
        $id = intval($this->request->getParam('id'));
        if ($id) {
            $data = DunGroupsModel::find($id);
            if (!$data) $this->ajaxresult(0, '未找到您要修改的信息');
            $this->view->assign('data', $data);
        }
        if ($this->ispost) {
            $post_data = array(
                'title' => $this->post('title'),
                'account' => $this->post('account'),
                'accessKeyId' => $this->post('accessKeyId'),
                'accessSecret' => $this->post('accessSecret'),
            );
            if ($data) {
                $status = $data->save($post_data);
            } else {
                $data = new DunGroupsModel();
                $status = $data->save($post_data);
            }

            if ($status == false) $this->ajaxresult(1, '添加失败');
            $this->ajaxresult(0, '处理成功');
        }
    }

}